package cn.tom.controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

//  不管那一层异常，统一由这个控制器处理
//  统一异常处理

// advice 通知
@ControllerAdvice
public class ErrController {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public String doIntegrityConstraintViolationException(Exception e, Model model){
    model.addAttribute("msg","主键编码异常");
    return "err";
}

    @ExceptionHandler(SQLException.class)
    public String doSQLException(Exception e, Model model){
        model.addAttribute("msg",e.getMessage());
        return "err";
    }
    @ExceptionHandler(IOException.class)
    public String doIOException(Exception e, Model model){
        model.addAttribute("msg","网路故障....."+e.getMessage());
        return "err";
    }
    @ExceptionHandler(Exception.class)
    public String doException(Exception e, Model model){
        model.addAttribute("msg",e.getMessage());
        return "err";
    }
}
